package model.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import model.Aluno;
import model.Disciplina;

public class DisciplinaDAO {
	
	private EntityManager manager = JPAUtil.getEntityManager();
	


	
	public static DisciplinaDAO getInstance(){
		return new DisciplinaDAO();
	}
	
	private DisciplinaDAO(){
		
	}
	
	public void adiciona(Disciplina disciplina){
		JPAUtil.iniciaTransacao();
		manager.persist(disciplina);
		JPAUtil.finalizaTransacao();
	}
	
	public Disciplina buscaDisciplina(int id){
		Disciplina disciplina = manager.find(Disciplina.class,id);
		return disciplina;
		
	}
	
	public Disciplina buscaDisciplinaPorNome(String nome){
		Query query = manager.createQuery("SELECT p FROM Disciplina p WHERE p.nome = ?");
		query.setParameter(1, nome);
		Disciplina disciplina = null;
		if (query.getResultList()!=null && !query.getResultList().isEmpty()){
			disciplina = (model.Disciplina) query.getResultList().get(0);
		}
		return disciplina;
		
	}
	
	public List<Disciplina> buscarTodosDisciplina(){
		Query query = manager.createQuery("SELECT p FROM Disciplina p");
		return query.getResultList();
	}
}
